package com.aiwei.tdjk.common.reposiotry.base;

import com.aiwei.tdjk.common.entity.search.Searchable;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * Created by konghao on 2016/12/7.
 */
@NoRepositoryBean
@Transactional
public interface BaseRepository<M,ID extends Serializable> extends JpaRepository<M,ID> {
    public List<Object[]> listBySQL(String sql);
    
    @Transactional
    public void updateBySql(String sql, Object... args);
    @Transactional
    public void updateByHql(String hql, Object... args);

    /**
     * 根据条件查询所有
     * 条件 + 分页 + 排序
     *
     * @param searchable
     * @return
     */
    Page<M> findAll(Searchable searchable);


    /**
     * 根据条件统计所有记录数
     *
     * @param searchable
     * @return
     */
    long count(Searchable searchable);







    public void deleteSelfAndChild(List<M> mList);

    public void saveappendChild(M parent, M child);

    /**
     * 移动节点
     * 根节点不能移动
     *
     * @param source   源节点
     * @param target   目标节点
     * @param moveType 位置
     */
    void move(M source, M target, String moveType);


    /**
     * 把源节点全部变更为目标节点
     *
     * @param source
     * @param newParentIds
     */
    void updateSelftAndChild(M source, ID newParentId, String newParentIds, int newWeight) ;

    /**
     * 查找目标节点及之后的兄弟  注意：值与越小 越排在前边
     *
     * @param parentIds
     * @param currentWeight
     * @return
     */
    List<M> findSelfAndNextSiblings(String parentIds, int currentWeight);


    /**
     * 查看与name模糊匹配的名称
     *
     * @param name
     * @return
     */
    public Set<String> findNames(Searchable searchable, String name, ID excludeId);


    /**
     * 查询子子孙孙
     *
     * @return
     */
    public List<M> findChildren(List<M> parents, Searchable searchable);

    public List<M> findAllByName(Searchable searchable, M excludeM);

    /**
     * 查找根和一级节点
     *
     * @param searchable
     * @return
     */
    public List<M> findRootAndChild(Searchable searchable);

    public Set<ID> findAncestorIds(Iterable<ID> currentIds);

    public Set<ID> findAncestorIds(ID currentId);

    /**
     * 递归查询祖先
     *
     * @param parentIds
     * @return
     */
    public List<M> findAncestor(String parentIds);


    public void addExcludeSearchFilter(Searchable searchable, M excludeM) ;

    public List<M> findAllWithNoPageNoSort(Searchable searchable) ;

    public List<M> findAllWithSort(Searchable searchable);
}
